<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺编号" prop="shopsId">
        <el-select v-model="queryParams.shopsId" placeholder="请选择商铺id" clearable>
          <el-option
            v-for="dict in shopsList"
            :key="dict.id"
            :label="dict.code"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租售类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择租售类别" clearable>
          <el-option
            v-for="dict in dict.type.zhwy_rental_contract_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租户姓名" prop="hirerId">
        <el-select v-model="queryParams.hirerId" placeholder="请选择租户姓名" clearable>
          <el-option
            v-for="dict in hirerList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click = "defaultRentCharge()">默认收费项(出租)</el-button>
    <el-button type="primary" @click = "defaultCharge()">默认收费项(出售)</el-button>
<!--    默认收费弹出框 -->
    <el-dialog
               :title="dtitle"
               :visible.sync="defaultdialogVisible"
               width="30%">
      <el-table  :data="defaultChargeData">
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="收费项名称" align="center" prop="name" />
        <el-table-column label="单价" align="center" prop="univalence" />
      </el-table>
    </el-dialog>

    <el-table v-loading="loading" :data="rentalcontractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商铺编号" align="center" prop="shopsId">
        <template slot-scope="scope">
          <dict-tag-shop :options="shopsList" :value="scope.row.shopsId"/>
        </template>
      </el-table-column>
      <el-table-column label="租售类别" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_rental_contract_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="租户姓名" align="center" prop="hirerId">
        <template slot-scope="scope">
          <dict-tag-hirer :options="hirerList" :value="scope.row.hirerId"/>
        </template>
      </el-table-column>
      <el-table-column label="租金" align="center" prop="rent" />
      <el-table-column label="行业" align="center" prop="trade" />
      <el-table-column label="详细说明" align="center" prop="remark" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出租时长" align="center" prop="rentTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_rental_contract_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rentalcontract:rentalcontract:edit']"
          >编辑合同</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租售合同管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-steps :active="number" >
              <el-step title="步骤 1" description="确认商铺信息"></el-step>
              <el-step title="步骤 2" description="选择业主"></el-step>
              <el-step title="步骤 3" description="设置费用"></el-step>
            </el-steps>
            <div v-show="number==1">
              <el-form-item label="商业区" prop="rentTime">
                <el-select v-model="form.shopsId" placeholder="请选择商业区">
                  <el-option
                    v-for="dict in shopsList"
                    :key="dict.id"
                    :label="dict.downtownName"
                    :value="parseInt(dict.id)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="编号" prop="type">
                <el-select v-model="form.shopsId" placeholder="请选择租售类别">
                  <el-option
                    v-for="dict in shopsList"
                    :key="dict.id"
                    :label="dict.code"
                    :value="parseInt(dict.id)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="租售类别" prop="type">
                <el-select v-model="form.type" placeholder="请选择租售类别">
                  <el-option
                    v-for="dict in dict.type.zhwy_rental_contract_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable
                                v-model="form.startTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择开始时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="出租时长" prop="rentTime">
                <el-input v-model="form.rentTime" placeholder="请输入出租时长" />
              </el-form-item>
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable
                                v-model="form.endTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择结束时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="租金" prop="rent">
                <el-input v-model="form.rent" placeholder="请输入租金" />
              </el-form-item>
              <div slot="footer" class="dialog-footer">
                <!--            <el-button type="primary" @click="submitForm">确 定</el-button>-->
                <el-button type="primary" @click="next1()">下一步</el-button>
              </div>
            </div>
          </el-form>
      <el-form ref="form" :model="hirerInfo" :rules="rules" label-width="80px">
            <div v-show="number==2">
              <el-form-item label="客户公司" prop="company">
                <el-input v-model="hirerInfo.company"  />
              </el-form-item>
              <el-form-item label="业主姓名" prop="name">
                <el-input v-model="hirerInfo.name"  />
              </el-form-item>
              <el-form-item label="身份证号" prop="card">
                <el-input v-model="hirerInfo.card"  />
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-select v-model="hirerInfo.sex" placeholder="请选择性别">
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="联系方式" prop="phone">
                <el-input v-model="hirerInfo.phone"  />
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="hirerInfo.email"  />
              </el-form-item>
              <div slot="footer" class="dialog-footer">
                <!--            <el-button type="primary" @click="submitForm">确 定</el-button>-->
                <el-button type="primary" @click="next()">上一步</el-button>
                <el-button type="primary" @click="next2()">下一步</el-button>
              </div>
            </div>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <div v-show="number==3">
              <el-button type="primary" @click="addCharge()">新增收费项目</el-button>
              <el-table  :data="chargeItemList" @selection-change="handleSelectionChange">
                <el-table-column label="序号" align="center" prop="id" />
                <el-table-column label="收费项名称" align="center" prop="name" />
                <el-table-column label="单价" align="center" prop="univalence" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-link type="danger" @click="deleteCharge(scope.row.id)">删除</el-link>
                  </template>
                </el-table-column>
              </el-table>
              <div slot="footer" class="dialog-footer">
                <!--            <el-button type="primary" @click="submitForm">确 定</el-button>-->
                <el-button type="primary" @click="next1()">上一步</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
              </div>
            </div>
      </el-form>
    </el-dialog>
    <!--    收费项目录表-->
    <el-dialog v-if="number==3 && dialogVisible == true"
      title="收费项目列表"
      :visible.sync="dialogVisible"
      width="30%">
      <el-table  :data="chargeList" @selection-change="select">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="收费项名称" align="center" prop="name" />
        <el-table-column label="单价" align="center" prop="univalence" />
      </el-table>
      <span slot="footer" class="dialog-footer">
         <el-button type="primary" @click="ensureCharge()">确定添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<!---->
<script>
import { listRentalcontract, getRentalcontract, delRentalcontract, addRentalcontract, updateRentalcontract } from "@/api/rentalcontract/rentalcontract";
import { listShops } from "@/api/shops/shops";
import { listHirer,getHirer} from "@/api/renter/hirer";
import { listRentalcontract_middle,delRentalcontract_middle,addRentalcontract_middle} from "@/api/rentalcontractMiddle/rentalcontract_middle";
import {listChargeitem,getChargeitem} from "@/api/charge/chargeitem";
import { listDefaultcharges, getDefaultcharges, delDefaultcharges, addDefaultcharges, updateDefaultcharges } from "@/api/defaultcharges/defaultcharges";
//
export default {
  name: "Rentalcontract",
  dicts: ['sys_user_sex', 'zhwy_rental_contract_state', 'zhwy_rental_contract_type'],
  data() {
    return {
      dtitle: null,
      defaultChargeData:[],
      defaultdialogVisible: false,
      //
      cid: [],
      //全部的收费项数据
      chargeList: [],
      //收费项选择弹出框
      dialogVisible: false,
      // 收费项中相关数据列表
      chargeItemList:[],
      // 租户信息
      hirerInfo:{},
      number: 1,
      rid: null,
      // 商铺数据列表
      shopsList:[],
      // 租户数据列表
      hirerList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租售合同管理表格数据
      rentalcontractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopsId: "",
        type: null,
        hirerId: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    defaultRentCharge(){
      this.defaultdialogVisible = true;
      this.getDefault();
      this.dtitle = "默认收费项(出租)"
    },
    defaultCharge(){
      this.defaultdialogVisible = true;
      this.getDefault();
      this.dtitle = "默认收费项(出售)"
    },
    getDefault(){
      if (this.dtitle == "默认收费项(出租)"){
        this.defaultChargeData.splice(0,this.defaultChargeData.length);
        listDefaultcharges({type:0}).then(resp=>{
          for (let i = 0; i<resp.rows.length;i++){
            getChargeitem(resp.rows[i].chargeitemId).then(res=>{
              this.defaultChargeData.push(res.data);
            })
          }
        })
      }else if(this.dtitle == "默认收费项(出售)"){
        this.defaultChargeData.splice(0,this.defaultChargeData.length);
        listDefaultcharges({type:1}).then(resp=>{
          for (let i = 0; i<resp.rows.length;i++){
            getChargeitem(resp.rows[i].chargeitemId).then(res=>{
              this.defaultChargeData.push(res.data);
            })
          }
        })
      }

    },
    //确认添加收费项数据
    ensureCharge(){
      this.dialogVisible = false;
      addRentalcontract_middle({rid: this.rid, cid: this.cid}).then(resp=>{
        this.$modal.msgSuccess("添加成功");
         this.getChargeInfo();
      })
    },
    //选中收费项时获取其id
    select(val){
      this.cid = val.map(function (item) {
        return item.id;
      });
    },
    addCharge(){
      this.dialogVisible = true;
      listChargeitem(this.queryParams).then(response=>{
        this.chargeList = response.rows;
      })
    },
    //删除合同中相对应的收费项
    deleteCharge(id){
     listRentalcontract_middle({chargeitemId:id}).then(resp=>{
        delRentalcontract_middle(resp.rows[0].id)
        this.getChargeInfo();
     })
    },
    //获取收费项中的数据
    getChargeInfo(){
      this.chargeItemList.splice(0, this.chargeItemList.length);
      listRentalcontract_middle({rentalcontractId:this.form.id}).then(resp =>{
        for (let i = 0; i < resp.rows.length;i++){
          getChargeitem(resp.rows[i].chargeitemId).then(res=>{
            this.chargeItemList.push(res.data);
          })
        }
      })
    },

    next(){
      this.number = 1;
    },
    next2(){
      this.number = 3;
      this.getChargeInfo();
    },
    next1(){
     this.number = 2;
        getHirer(this.form.hirerId).then(res =>{
         this.hirerInfo = res.data
        })
    },
    //
    /** 查询租售合同管理列表 */
    getList() {
      this.loading = true;
      listRentalcontract(this.queryParams).then(response => {
        this.rentalcontractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listShops(this.queryParams).then(response => {
        this.shopsList = response.rows;
      });
      listHirer(this.queryParams).then(response => {
        this.hirerList = response.rows;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopsId: "",
        type: null,
        hirerId: "",
        rent: null,
        trade: null,
        remark: null,
        startTime: null,
        rentTime: null,
        endTime: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租售合同管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.number = 1;
      const id = row.id || this.ids
      getRentalcontract(id).then(response => {
        this.form = response.data;
        this.rid = id;
        this.open = true;
        this.title = "编辑合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRentalcontract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRentalcontract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除租售合同管理编号为"' + ids + '"的数据项？').then(function() {
        return delRentalcontract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rentalcontract/rentalcontract/export', {
        ...this.queryParams
      }, `rentalcontract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
